<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>


<c:if test="${dataSource==null}">
    <sql:setDataSource var="dataSource"
                       driver="sun.jdbc.odbc.JdbcOdbcDriver"
                       url="jdbc:odbc:forum"
                       scope="session"/>
    
</c:if>

<c:if test="${pageContext.request.method=='POST'}">
    <c:if test="${param.reg!=null}">
        <c:redirect url="register.jsp" />
    </c:if>
    
    <sql:query var="users" dataSource="${dataSource}">
        select c_uid,c_type from t_users where c_uid = ? and c_pwd = ? 
        <sql:param value="${param.uid}" />
        
        <sql:param value="${param.pwd}" />
    </sql:query>
    
    <c:choose>
        <c:when test="${users.rowCount<1}">
            <h3 color="red">Sorry, we have no one registered with that
            name.</h3>
            
            <sql:update var="result" dataSource="${dataSource}">
                update t_users set c_bad = c_bad + 1 where c_uid = ? 
                <sql:param value="${param.uid}" />
            </sql:update>
        </c:when>
        
        <c:otherwise>
            <%
            Cookie mycookie = new Cookie("login", request.getParameter("uid"));
            mycookie.setMaxAge(0x7ffffff);
            response.addCookie(mycookie);
            %>
            <c:forEach var="aUser" items="${users.rows}">
                <c:set var="userID" value="${aUser.c_uid}" scope="session" />
                
                <c:set var="userType" value="${aUser.c_type}"
                       scope="session" />
            </c:forEach>
            
            <c:redirect url="welcome.jsp" />
        </c:otherwise>
    </c:choose>
</c:if>

<c:if test="<%=request.getCookies()!=null%>">
    <c:forEach var="aCookie" items="<%=request.getCookies()%>">
        <c:if test="${aCookie.name=='login'}">
            <c:set var="uid" value="${aCookie.value}" />
        </c:if>
    </c:forEach>
</c:if>

<html>
    <head>
        <title>Welcome</title>
    </head>
    
    <body>
        Welcome to the forum. Please login.<br>
        <form method="POST">
            <table border="1" cellpadding="0" cellspacing="0"
                   style="border-collapse: collapse" bordercolor="#111111"
                   width="49%" id="AutoNumber1">
                <tr>
                    <td width="100%" colspan="2" bgcolor="#0000FF">
                        <p align="center">
                            <b>
                                <font color="#FFFFFF" size="4">Login to
                                Forum</font>
                            </b>
                        </p>
                    </td>
                </tr>
                
                <tr>
                    <td width="26%">Login Name</td>
                    
                    <td width="74%">
                        <input type="text" name="uid" value="<c:out value="${uid}"/>" size="20" />
                        
                    </td>
                </tr>
                
                <tr>
                    <td width="26%">Password</td>
                    
                    <td width="74%">
                        <input type="password" name="pwd" size="20" />
                    </td>
                </tr>
                
                <tr>
                    <td width="100%" colspan="2">
                        <p align="center">
                            <input type="submit" value="Login" name="Login" />
                            
                            <input type="submit" value="Register" name="reg" />
                        </p>
                    </td>
                </tr>
            </table>
            
            <p>&#160;</p>
        </form>
        
        <p>Note: use id of 
            <b>admin
            </b> and password of 
            <b>admin
            </b> on first use.
        </p>
    </body>
</html>

